<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:p="http://primefaces.org/ui">

    <ui:composition template="/template.xhtml">
        <ui:define name="body">
            <h:form id="TbInstListForm">
                <p:breadCrumb>
                    <p:menuitem value="Home" url="#" />
                    <p:menuitem value="Cadastro" url="#" />
                    <p:menuitem value="Cliente" url="#" />
                </p:breadCrumb>            
                <p:panel>
                    <p:dataTable id="datalist" 
                                 emptyMessage="Nenhum registro localizado"
                                 scrollable="true"
                                 scrollWidth="98%"
                                 widgetVar="FiltroGlobal"
                                 value="#{tbInstController.items}" 
                                 var="item"
                                 selectionMode="single" 
                                 selection="#{tbInstController.selected}"
                                 paginator="true"
                                 rowKey="#{item.codInstituicao}"
                                 rows="10"
                                 paginatorPosition="bottom"
                                 rowsPerPageTemplate="10,20,30,40,50">

                        <p:ajax event="rowSelect"   update="createButton viewButton viewButtonTop editButton editButtonTop deleteButton deleteButtonTop"/>
                        <p:ajax event="rowUnselect" update="createButton viewButton viewButtonTop editButton editButtonTop deleteButton deleteButtonTop"/>

                        <f:facet name="header">
                            <p:outputPanel>
                                <p:inputText id="globalFilter" style="width:200px;" placeholder=""/>
                                <p:commandButton value="Filtrar" id="withIcon" onclick="PF('FiltroGlobal').filter()" icon="ui-icon-search" />
                                <p:commandButton id="toggler" type="button" value="Colunas" style="float:right" icon="ui-icon-calculator" rendered="false" />
                                <p:columnToggler datasource="datalist" trigger="toggler" />                                

                                <p:commandButton id="createButtonTop" icon="ui-icon-plus" value="#{bundle.Create}" actionListener="#{tbInstController.prepareCreate}" update=":TbInstCreateForm" oncomplete="PF('TbInstCreateDialog').show()"/>
                                <p:commandButton id="viewButtonTop"   icon="ui-icon-search" value="#{bundle.View}" update=":TbInstViewForm" oncomplete="PF('TbInstViewDialog').show()" disabled="#{empty tbInstController.selected}"/>
                                <p:commandButton id="editButtonTop"   icon="ui-icon-pencil" value="#{bundle.Edit}" update=":TbInstEditForm" oncomplete="PF('TbInstEditDialog').show()" disabled="#{empty tbInstController.selected}"/>
                                <p:commandButton id="deleteButtonTop" icon="ui-icon-trash" value="#{bundle.Delete}" actionListener="#{tbInstController.destroy}" update=":growl,datalist" disabled="#{empty tbInstController.selected}">
                                    <p:confirm header="Confirmação" message="Confirma a exclusão?" icon="ui-icon-alert"/>
                                </p:commandButton>
                                <p:confirmDialog global="true">
                                    <p:commandButton value="Sim" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check"/>
                                    <p:commandButton value="Não" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close"/>
                                </p:confirmDialog>
                            </p:outputPanel>
                        </f:facet>                        
                        <p:column filterBy="#{item.codInstituicao}" filterMatchMode="contains" width="120px;">
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListTbInstTitle_codInstituicao}"/>
                            </f:facet>
                            <h:outputText value="#{item.codInstituicao}"/>
                        </p:column>
                        <p:column filterBy="#{item.nomFantasia}" filterMatchMode="contains"> 
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListTbInstTitle_nomFantasia}"/>   
                            </f:facet>
                            <h:outputText value="#{item.nomFantasia}"/>
                        </p:column>
                        <p:column filterBy="#{item.desFone1.replaceAll('\\(|\\)|\\-| ','')}" filterMatchMode="contains" width="150px;"> 
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListTbInstTitle_desFone1}"/>
                            </f:facet>
                            <h:outputText value="#{item.desFone1}" />
                        </p:column>
                        <p:column filterBy="#{item.desEmail}" filterMatchMode="contains" width="150px;"> 
                            <f:facet name="header">
                                <h:outputText value="#{bundle.ListTbInstTitle_desEmail}"/>
                            </f:facet>
                            <h:outputText value="#{item.desEmail}"/>
                        </p:column>
                        <p:column filterBy="#{(item.visitasList.size() != 0) ? 'S': 'N'}" filterMatchMode="equals">
                            <f:facet name="header">
                                <h:outputText value="Em visita"/>
                            </f:facet>                            
                            <f:facet name="filter">
                                <p:selectOneButton onchange="PF('FiltroGlobal').filter()">
                                    <f:selectItem itemLabel="Todos" itemValue="" />
                                    <f:selectItem itemLabel="Sim" itemValue="S" />
                                    <f:selectItem itemLabel="Não" itemValue="N" />
                                </p:selectOneButton>
                            </f:facet>                            
                            <div style="display:block;width:500px;">
                                <p:commandButton id="createVisitasButton" icon="ui-icon-plus"   value="Iniciar Visita" actionListener="#{visitasController.prepareCreate}" update=":TbInstListForm,:VisitasCreateForm,datalist" oncomplete="PF('VisitasCreateDialog').show()" rendered="#{item.visitasList.size() == 0 and sessionScope.user.funcao != 'Secretária'}" style="margin:0;width:127px;" >
                                    <f:setPropertyActionListener value="#{item}" target="#{visitasController.selectedCliente}"/>
                                </p:commandButton>          
                                <p:commandButton id="encerrarVisitasButton" icon="ui-icon-minus" value="Encerrar Visita" update=":TbInstListForm,:VisitasEncerrarForm,datalist" oncomplete="PF('VisitasEncerrarDialog').show()" rendered="#{item.visitasList.size() != 0 and sessionScope.user.funcao != 'Secretária'}" style="margin:0;width:127px;" >
                                    <f:setPropertyActionListener value="#{item}" target="#{visitasController.selectedCliente}" />
                                    <f:setPropertyActionListener value="#{item.visitasList.get(0)}" target="#{visitasController.selectedEncerrar}" />
                                </p:commandButton>  
                                <p:commandButton value="Visita" update=":CliViewVisitasDialogForm" oncomplete="PF('CliViewVisitasDialog').show()" icon="ui-icon-search" title="Visita" style="margin:0">
                                    <f:setPropertyActionListener value="#{item}" target="#{visitasController.selectedCliente}" />
                                </p:commandButton>    
                                <p:commandButton value="Evento" update=":CliViewEventosDialogForm" oncomplete="PF('CliViewEventosDialog').show()" icon="ui-icon-search" title="Evento" style="margin:0">
                                    <f:setPropertyActionListener value="#{item}" target="#{sigifEventosintController.selectedCliente}" />
                                </p:commandButton>    
                                <p:commandButton value="SAC" update=":CliViewSACDialogForm" oncomplete="PF('CliViewSACDialog').show()" icon="ui-icon-search" title="SAC" style="margin:0">
                                    <f:setPropertyActionListener value="#{item}" target="#{sacController.selectedCliente}" />
                                </p:commandButton>
                                <p:commandButton value="Ficha" update=":CliViewFichaDialogForm" oncomplete="PF('CliViewFichaDialog').show()" icon="ui-icon-search" title="Ficha" style="margin:0">
                                    <f:setPropertyActionListener value="#{item}" target="#{sacController.selectedCliente}" />
                                    <f:setPropertyActionListener value="#{item}" target="#{visitasController.selectedCliente}" />
                                    <f:setPropertyActionListener value="#{item}" target="#{sigifEventosintController.selectedCliente}" />
                                </p:commandButton>
                            </div>
                        </p:column>                             
                        <f:facet name="footer">
                            <p:commandButton id="createButton" icon="ui-icon-plus"   value="#{bundle.Create}" actionListener="#{tbInstController.prepareCreate}" update=":TbInstCreateForm" oncomplete="PF('TbInstCreateDialog').show()"/>   
                            <p:commandButton id="viewButton" icon="ui-icon-search" value="#{bundle.View}" update=":TbInstViewForm" oncomplete="PF('TbInstViewDialog').show()" disabled="#{empty tbInstController.selected}"/>
                            <p:commandButton id="editButton" icon="ui-icon-pencil" value="#{bundle.Edit}" update=":TbInstEditForm" oncomplete="PF('TbInstEditDialog').show()" disabled="#{empty tbInstController.selected}"/>
                            <p:commandButton id="deleteButton" icon="ui-icon-trash" value="#{bundle.Delete}" actionListener="#{tbInstController.destroy}" update=":growl,datalist" disabled="#{empty tbInstController.selected}">
                                <p:confirm header="Confirmação" message="Confirma a exclusão?" icon="ui-icon-alert"/>
                            </p:commandButton>
                            <p:confirmDialog global="true">
                                <p:commandButton value="Sim" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check"/>
                                <p:commandButton value="Não" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close"/>
                            </p:confirmDialog>
                        </f:facet>                    
                    </p:dataTable>
                </p:panel>
            </h:form>

            <ui:include src="Create.xhtml"/>
            <ui:include src="View.xhtml"/>
            <ui:include src="Edit.xhtml"/>
            <ui:include src="CreateVisita.xhtml"/>
            <ui:include src="EncerrarVisita.xhtml"/>
            <ui:include src="ViewVisitas.xhtml"/>
            <ui:include src="ViewEventos.xhtml"/>
            <ui:include src="ViewSAC.xhtml"/>
            <ui:include src="ViewFicha.xhtml"/>
        </ui:define>
    </ui:composition>

</html>
